<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/8/16
 * Time: 18:16
 */

namespace app\models;

use Yii;
use \common\helps\tools;//使用工具类
use \app\models\SpecialProject;
use \app\models\Table;

//special model
class Special extends BaseModel
{
    /**
     * title 表名
     * @var string
     */
    protected $table = 'd_special_manage';


    /**
     * title 单条查找
     * @param $id
     * @return array|bool
     */
    public function findManage($id)
    {
        $query = new \yii\db\Query();
        $result = $query->select(['id','status','user_id','check_think','child_id','project_name','create_time','update_time'])->from(tools::databaseName().'.'.$this->table)->where(['id' => $id])->one();
        return $result;
    }




        /**
     * title 专家待验收列表
     * @param $uid
     * @return array
     */
    public function checkList($uid){
        $query = new \yii\db\Query();
        $list = $query->select(['a.id as manage_id','a.child_id','b.item_no','b.project_name','c.begin_time','c.end_time','b.belong_company','c.project_type'])
            ->from('data_yii.d_special_manage AS a')
            ->rightJoin('data_yii.special_project_child AS b', 'b.id = a.child_id')
            ->rightJoin('data_yii.special_project AS c', 'b.project_id = c.id')
            ->where(['a.user_id' => $uid,'a.status' => 2])
            ->all();
        return $list;

    }


    /**
     * 验收项目 编辑 详情
     * @param $manage_id
     * @return array|int
     */
    public static function checkSpecial($manage_id)
    {
        $data = [];
        $Special = new Special;
        $findManage =$Special->findManage($manage_id);
        $data['manage'] = $findManage;//验收表

        //调用user
        $user = new \app\models\User;
        $findUser = $user->findUser($findManage['user_id']);
        if(!$findUser){
            return 2;
        }
        $data['user'] = $findUser;

        //调用项目
        $childProject = SpecialProject::childProject($findManage['child_id']);
        if(!$childProject){
            return 2;
        }
        $data['project'] = $childProject;

        //调用table
        $child_id = $findManage['child_id'];
        $childLine = Table::childLine($child_id);
        if(empty($childLine)){
            $child_id = 1;
            $childLine = Table::childLine($child_id);

        }
        $data['table'] = $childLine;
        $data['table']['detail'] = Table::tableDetail($child_id);

        //调用

        return $data;

    }



    public function checkSave($user,$manage,$table,$detail)
    {
        $manage_id = $manage['id'];
        $update_time = date('Y-m-d H:i:s',time());
        //列表 manage查询
        $result = $this->findManage($manage_id);

        //列表 manage修改
        $check_think = $manage['check_think'];
        $this->update_data = ['check_think'=>$check_think,'update_time'=>$update_time];
        $saveMange = $this->save(tools::databaseName().'.'.$this->table,$this->update_data,"id='$manage_id'");
        if(!$saveMange){
            return 2;
        }

        //user表
        $name = $user['name'];
        $phone = $user['phone'];
        $email = $user['email'];
        $company = $user['company'];
        $job = $user['job'];
        $this->update_data = ['name'=>$name,'phone'=>$phone,'email'=>$email,'company'=>$company,'job'=>$job,'update_time'=>$update_time];
        $saveUser = $this->save(tools::databaseName().'.d_user',$this->update_data,"id='".$result['user_id']."'");
        if(!$saveUser){
            return 2;
        }

        //talbe表
        $childTable = table::childLine($result['child_id']);
        if($childTable){
//            detailChild
        }

        $this->update_data = [['content'=>'3123','pid'=>'0','level'=>'1','order'=>'1'],['content'=>'55555','pid'=>'0','level'=>'2','order'=>'1']];
        $saveUser = $this->save(tools::databaseName().'.d_user',$this->update_data,"id='".$result['user_id']."'");
        if(!$saveUser){
            return 2;
        }






    }







}